import Vue from 'vue'
import VueRouter from 'vue-router'
import { Routes } from './config'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
// 解决 replace 重定向 跳转报错
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err)
}
const routes = [
    {
        path: "/",
        redirect: '/login',
    }
]

const router = new VueRouter({
    routes
})
// const userId = localStorage.getItem('user-id');
router.addRoutes(Routes)
router.beforeEach((to, from, next) => {
    const { meta, path } = to;
    if (meta && meta.title) {
        document.title = meta.title;
    }

    // 是否登录状态
    let userId = localStorage.getItem('user-id');
    let userInfo = localStorage.getItem('userInfo');
    console.log(userId,path)
    if (!userId && path == '/home') {
        next({ path: '/login' });
        localStorage.clear();
        return
    }

    if (!userInfo && path == '/mgr/home') {
        console.log(1)
        next({ path: '/mgr/login' });
        localStorage.clear();
        return
    }

    next()
})
export default router
